package com.wbj.vpdirection.recycle

import android.content.Context
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import android.widget.Toast
import androidx.recyclerview.widget.RecyclerView
import com.wbj.vpdirection.R
import com.wbj.vpdirection.mode.AngleData

class DirectionAdapter(val context: Context, private val list: MutableList<AngleData>) :
    RecyclerView.Adapter<DirectionAdapter.DirectionViewHolder>() {
    
    
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): DirectionViewHolder {
        return DirectionViewHolder(
            LayoutInflater.from(context).inflate(R.layout.item_direct, parent, false)
        )
    }
    
    override fun onBindViewHolder(holder: DirectionViewHolder, position: Int) {
        val realPosition = position % list.size
        val angle = list[realPosition]
        holder.itemView.setOnClickListener {
            Toast.makeText(context, "$realPosition", Toast.LENGTH_SHORT).show()
        }
        holder.tvLeftAngle.text = angle.leftAngle.toString()
        holder.tvRightAngle.text = angle.rightAngle.toString()
        val centerTextView = holder.tvCenterAngle
        centerTextView.text = if (!TextUtils.isEmpty(angleMode[angle.centerAngle])){
            angleMode[angle.centerAngle]
        }else {
            angle.toString()
        }
        centerTextView.isSelected = angle.isSelect
    }
    
    override fun getItemCount(): Int {
        return list.size
    }
    
    
    class DirectionViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        var tvLeftAngle: TextView = itemView.findViewById(R.id.tvLeftAngle)
        var tvCenterAngle: TextView = itemView.findViewById(R.id.tvCenterAngle)
        var tvRightAngle: TextView = itemView.findViewById(R.id.tvRightAngle)
    }
}